package com.anysoft.other;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.anysoft.MentorConst;
import com.anysoft.utils.MentorLogger;
import com.blankj.utilcode.util.FileUtils;
import com.anysoft.HostConstant;
import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.GetStartupAdvertisementBean;
import com.anysoft.guide.GuideCountDownNewStyleActivity;
import com.anysoft.guide.GuideNewActivity;
import com.anysoft.home.HomeActivity;
import com.anysoft.me.config.ServiceConfig;
import com.anysoft.me.utils.NetWorkUtil;
import com.anysoft.me.utils.PermissionManager;
import com.anysoft.utils.AESUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.DiskUtils;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.PreferencesUtils;
import com.anysoft.utils.StringUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SplashNewActivity extends BaseActivity implements PermissionManager.OnPermissionListener {

    private static final int COUNT_DOWN_SECOND = 2;

    public static String UUID = "";
    @BindView(R.id.tv_bottom)
    TextView tvBottom;

    private boolean isLoadSuccess = false;
    private boolean deleteFilesInDir;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        ButterKnife.bind(this);
        firstEvent();
    }

    private void firstEvent() {
        if (PermissionManager.getInstance().checkPermissions(this, PermissionManager.PERMISSION_ALL_STATE)) {
            UUID = getUUID();
            if (PreferencesUtils.getBoolean(this, MentorConst.IS_FIRST_COME, true)) {
                deleteFilesInDir = FileUtils.deleteFilesInDir(DiskUtils.CACHE_PATH);
                MentorLogger.Loge("onCreate: 删除本地文件 " + deleteFilesInDir);
            }
            startRun();
        } else {
            PermissionManager.getInstance().requestPermissions(this, PermissionManager.REQUEST_ALL_STATE,
                    PermissionManager.PERMISSION_ALL_STATE, this);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onPermissionSuccess() {
        UUID = getUUID();
        startRun();
    }

    @Override
    public void onPermissionFail() {
        startRun();
    }

    private void startRun() {
        countDowntoStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onPause();
    }

    private boolean isCacheSplashAD = false;//广告缓存标识

    private GetStartupAdvertisementBean advertiseBean;

    private Timer timer;

    private void countDowntoStart() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                launchActivity();
            }
        }, COUNT_DOWN_SECOND * 1000);// 设定指定的时间time
    }

    private void launchActivity() {
        if (PreferencesUtils.getBoolean(this, MentorConst.IS_FIRST_COME, true)) {
            //先展示广告引导页
            startActivity(new Intent().setClass(mActivity, GuideNewActivity.class));
        } else {
            if (isLoadSuccess) {
                Intent intent = new Intent().setClass(mActivity, GuideCountDownNewStyleActivity.class);
                intent.putExtra("advertiseBean", advertiseBean);
                startActivity(intent);
            } else {
                startActivity(new Intent().setClass(mActivity, HomeActivity.class));
            }
        }
        finish();
    }

    @OnClick(R.id.tv_bottom)
    public void onClick() {
        startRun();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}
